<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.keyword" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getDictionarys">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="dictionarys" scope="scope" highlight-current-row v-loading="listLoading" @cell-dblclick="changeDrawer" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>

		<!--	<el-button @click="drawer = true" type="primary" style="margin-left: 16px;">-->
				<el-table-column  prop="name" label="姓名" width="420"  type="primary" style="margin-left: 16px;" sortable>
				</el-table-column>
			<!--</el-button>-->

			<el-table-column prop="sn" label="编号" width="420"  sortable>
			</el-table-column>
			<el-table-column label="操作" >
				<template scope="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--抽屉-->
		<el-drawer
				title="数据字典明细"
				:visible.sync="drawer"
				:direction="direction"
				:before-close="handleClose"
		:size="550">
			<!--工具条-->
			<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
				<el-form :inline="true" :model="filters">
					<el-form-item>
						<el-input v-model="defilters.keyword" placeholder="姓名"></el-input>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" v-on:click="deserach">查询</el-button>
					</el-form-item>
					<el-form-item>
						<el-button type="primary" @click="handleDeAdd">新增</el-button>
					</el-form-item>
				</el-form>
			</el-col>
			<el-table :data="details" scope="scope" highlight-current-row v-loading="listLoading"  @selection-change="selsChange" style="width: 100%;">
				<el-table-column type="selection" width="55">
				</el-table-column>

				<el-table-column prop="id" label="编号" width="120"  sortable>
				</el-table-column>
				<el-table-column  prop="name" label="明细名" width="120"  type="primary" style="margin-left: 16px;" sortable>
				</el-table-column>



				<el-table-column label="操作" >
					<template scope="scope">
						<el-button size="small" @click="DehandleEdit(scope.$index, scope.row)">编辑</el-button>
						<el-button type="danger" size="small" @click="DehandleDel(scope.$index, scope.row)">删除</el-button>

					</template>
				</el-table-column>
			</el-table>

			<el-pagination layout="prev, pager, next"
						   @current-change="handleDeCurrentChange"
						   :current-page="DecurrentPage"
						   :page-size="DepageSize"
						   :total="Detotal"
						   style="float:right;">
			</el-pagination>
		</el-drawer>
		<!--抽屉结束-->


		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="prev, pager, next"
						   @current-change="handleCurrentChange"
						   :current-page="currentPage"
						   :page-size="pageSize"
						   :total="total"
						   style="float:right;">
			</el-pagination>
		</el-col>

		<!-- Detial新增修改界面-->
		<el-dialog :title="title" :visible.sync="addDeFormVisible" :close-on-click-modal="false">
			<el-form :model="addDeForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="明细名" prop="sn">
					<el-input v-model="addDeForm.name" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addDeFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addDeSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>




		<!--新增界面-->
		<el-dialog :title="title" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
				<el-form-item label="字典名" prop="name">
					<el-input v-model="addForm.name" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="字典编号" prop="sn">
					<el-input v-model="addForm.sn" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	import util from '../../common/js/util'
	//import NProgress from 'nprogress'
	import { getDictionaryListPage, removeDictionary, batchRemoveDictionary, editDictionary, addDictionary } from '../../api/api';

	export default {
		data() {
			return {
				filters: {
					keyword: ''

				},
				defilters: {
					keyword:''
				},
				drawer: false,
				direction: 'rtl',
				dictionarys: [],
				details:[],
				currentPage:1,
				pageSize:5,
				total: 0,
				DecurrentPage:1,
				DepageSize:2,
				Detotal: 0,
				currentId:0,
				title:'',
				page: 1,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
				editFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},

				addFormVisible: false,//新增界面是否显示
				addDeFormVisible:false,
				addLoading: false,
				addFormRules: {
					name: [
						{ required: true, message: '请输入姓名', trigger: 'blur' }
					]
				},
				addDeForm:{
					id:null,
					name:'',
					typesId:this.currentId
				},
				//新增界面数据
				addForm: {
					id:null,
					name: '',
					sn: ''
				}

			}
		},
		methods: {
			changeDrawer(row){
				let id = row.id;
				this.currentId = id;
				this.getDetail(id);
				this.drawer=true;
			},
			handleCurrentChange(val) {
				this.currentPage = val;
				this.getDictionarys();
			},
			handleDeCurrentChange(val) {
				this.DecurrentPage = val;
				let id = this.currentId;
				this.getDetail(id);
			},
			//获取用户列表
			getDictionarys() {
				let para = {
					currentPage: this.currentPage,
					keyword: this.filters.keyword,
					pageSize: this.pageSize
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/dic",para)
					.then(result=>{
						this.listLoading = false;
						this.dictionarys = result.data.rows;
						this.total = result.data.totals;
					})
				/*getDictionaryListPage(para).then((res) => {
					this.total = res.data.total;
					this.dictionarys = res.data.dictionarys;
					this.listLoading = false;
					//NProgress.done();
				});*/
			},
			DehandleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/det/"+row.id)
							.then(result=>{
								this.listLoading = false;
								result = result.data;
								if(result.success){
									this.$message({
										message: result.message,
										type: 'success'
									});
									this.DecurrentPage = 1;
									this.getDetail(this.currentId);
								}else{
									this.$message({
										message: result.message,
										type: 'err'
									});
									this.listLoading = false;
									this.getDetail(this.currentId);
								}
							})
							.catch(result=>{
								this.listLoading = false;
							})
					//NProgress.start();
					//let para = { id: row.id };
					/*removeDictionary(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDictionarys();
					});*/
				}).catch(() => {

				});
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/dic/"+row.id)
							.then(result=>{
								this.listLoading = false;
								result = result.data;
								if(result.success){
									this.$message({
										message: result.message,
										type: 'success'
									});
									this.currentPage = 1;
									this.getDictionarys();
								}else{
									this.$message({
										message: result.message,
										type: 'err'
									});
									this.listLoading = false;
									this.getDictionarys();
								}
							})
							.catch(result=>{
								this.listLoading = false;
							})
					//NProgress.start();
					//let para = { id: row.id };
					/*removeDictionary(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDictionarys();
					});*/
				}).catch(() => {

				});
			},
			//显示De编辑界面
			handleEdit: function (index, row) {
				this.title = "编辑";
				this.addFormVisible = true;
				this.addForm = Object.assign({}, row);

			},
			//=========================
			//显示编辑界面
			DehandleEdit: function (index, row) {
				this.title = "编辑";
				this.addDeFormVisible = true;
				this.addDeForm = Object.assign({}, row);
				this.addDeForm = {
					id:row.id,
					name: row.name,
					sn: row.sn,
					typesId:this.currentId
				};
			},
			//显示新增界面
			handleDeAdd: function () {
				this.title = "新增";
				this.addDeFormVisible = true;
				this.addDeForm = {
					id:null,
					name: '',
					sn: '',
					typesId:this.currentId
				};
			},
			deserach(){
				let para = {
					currentPage: this.DecurrentPage,
					keyword: this.defilters.keyword,
					id:this.currentId,
					pageSize: this.DepageSize
				};
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/det",para)
						.then(result=>{
							this.listLoading = false;
							this.details= result.data.rows;
							this.Detotal = result.data.totals;
						})
			},
			addDeSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addDeForm);
							//para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							this.$http.put("/det",para)
									.then(result=>{
										this.addLoading = false;
										result = result.data;
										if(result.success){
											this.$message({
												message: result.message,
												type: 'success'
											});
											this.DecurrentPage = 1;
											this.getDetail(this.currentId);
											this.addDeFormVisible = false;
										}else{
											this.$message({
												message: result.message,
												type: 'err'
											});
											this.DecurrentPage = 1;
											this.getDetail(this.currentId);
										}
									})
									.catch(result=>{
									})
						});
					}
				});
			},
			//显示新增界面
			handleAdd: function () {
				this.title = "新增";
				this.addFormVisible = true;
				this.addForm = {
					id:null,
					name: '',
					sn: ''
				};
			},

			//编辑
			editSubmit: function () {
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.editLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.editForm);
							para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							editDictionary(para).then((res) => {
								this.editLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['editForm'].resetFields();
								this.editFormVisible = false;
								this.getDictionarys();
							});
						});
					}
				});
			},
			//新增或修改
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							//NProgress.start();
							let para = Object.assign({}, this.addForm);
							//para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
							this.$http.put("/dic",para)
								.then(result=>{
									this.addLoading = false;
									result = result.data;
									if(result.success){
										this.$message({
											message: result.message,
											type: 'success'
										});
										this.currentPage = 1;
										this.getDictionarys();
										this.addFormVisible = false;
										this.$refs['editForm'].resetFields();
									}else{
										this.$message({
											message: result.message,
											type: 'err'
										});
										this.currentPage = 1;
										this.getDictionarys();
										this.$refs['editForm'].resetFields();
									}
								})
								.catch(result=>{

								})
							/*addDictionary(para).then((res) => {
								this.addLoading = false;
								//NProgress.done();
								this.$message({
									message: '提交成功',
									type: 'success'
								});
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getDictionarys();
							});*/
						});
					}
				});
			},
			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id).toString();
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					let para = { ids: ids };
					batchRemoveDictionary(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDictionarys();
					});
				}).catch(() => {

				});
			},
			handleClose(done) {
				this.$confirm('确认关闭？')
						.then(_ => {
							done();
						})
						.catch(_ => {});
			},
			getDetail(id){
				let para = {
					currentPage: this.DecurrentPage,
					pageSize:this.DepageSize,
					keyword:this.defilters.keyword,
					id: id
				};
				this.$http.post("/det",para).then(result=>{
					this.details = result.data.rows;
					this.Detotal = result.data.totals;
					console.log(result.data);
				})
			}
		},
		mounted() {
			this.getDictionarys();
		}
	}

</script>

<style scoped>

</style>